import { configTemplateType } from "../type";

/**
 * 弹出讲解商品的方法
 */
export async function popUpGoodsPop(config: configTemplateType) {
  let index = config.serialNum - 1;
  let goodsNodes = document.querySelectorAll('div[class*="goodsItem-"]');
  // 商品的index
  let goodsBtnNodes = goodsNodes[index].querySelectorAll(
    'div[class*="goodsAction-"] button'
  ) as unknown as HTMLElement[];
  console.log(goodsBtnNodes, "goodsNodes");
  // 商品中要操作按钮的index
  let btnIndex = 0;
  // console.log(goodsBtnNodes, "goodsBtnNodes");
  let btnText = goodsBtnNodes[btnIndex].innerText;
  if (btnText == "取消讲解") {
    goodsBtnNodes[btnIndex].click();
  }
  await setExplanation(goodsBtnNodes, btnIndex, 0);
}

/**
 * 发送并置顶指定的消息
 */
export async function sendMessageAndTop(config: configTemplateType) {
  let inputNode = document.querySelector(
    'div[class*="inputSection"] textarea'
  ) as HTMLElement;
  console.log(inputNode, "inputNode");
  inputNode.click();
  if (config.commentText) {
    insertTextWithCommand(inputNode, config.commentText);
  } else {
    let index = config.serialNum - 1;
    await sleep(1000);
    let contentNodes = document.querySelectorAll(".auxo-dropdown-menu-item");
    let curNode = contentNodes[index].querySelector(
      "div[class*='content-']"
    ) as HTMLElement;
    curNode.click();
  }

  // 判断是否进行置顶操作
  if (config.isTop) {
    let commentNode = document.querySelector(
      'label[class*="commentOperations_settop_checkbox"]'
    ) as HTMLElement;
    commentNode.click();
  }
  await sleep(1000);

  // 点击发布评论
  let submitNode = document.querySelector(
    'div[class*="inputSuffix"]'
  ) as HTMLElement;
  submitNode.click();
}

/**
 * 切换当前获取数据类型 近一小时 累计
 */
export async function switchTimeData(index = 2) {
  let liveDataToTiemNodes = document.querySelectorAll(
    'div[class*="optionItem-"]'
  ) as unknown as HTMLElement[];
  liveDataToTiemNodes[index].click();
}
/**
 * 打开福袋弹框
 */
export async function openLuckyBigTool() {
  let liveToolsNode = document.querySelector(
    'div[class*="liveTools-"]'
  ) as HTMLElement;
  // 福袋控件Node节点
  let luckyBigToolsNode = liveToolsNode.children[0] as HTMLElement;
  // 打开福袋控件抽屉
  luckyBigToolsNode.click();
}
/**
 * 切换福袋列表到全部
 */
export async function switchLuckyBigToolTabs(index = 0) {
  // 弹框中的tab
  let popTabs = document.querySelectorAll(
    ".buyin-tabs-tab"
  ) as unknown as HTMLElement[];
  popTabs[index].click();
}

/**
 * 切换福袋列表到全部
 */
export async function switchLuckyBigListPage(page = 1) {
  // 弹框中的tab
  let pageTab = document.querySelector(`li[title="${page}"]`) as HTMLElement;
  pageTab.click();
}
/**
 * 关闭福袋弹框
 */
export function closeLuckyBigPop() {
  let LuckyBigPop = document.querySelector(
    '.buyin-drawer-content button[aria-label="Close"]'
  ) as HTMLElement;
  console.log(LuckyBigPop, "LuckyBigPop");

  if (LuckyBigPop) {
    LuckyBigPop.click();
  }
}

export async function releaseLuckyBigByIndex(index = 0) {
  let superLuckyBagDrawerNode = (await getLuckyBagDrawerNode(0)) as HTMLElement;
  let luckyBagCardNodes = (await getLuckyBagCardNodes(
    superLuckyBagDrawerNode,
    0
  )) as HTMLElement[];

  let luckyBagCardNode = luckyBagCardNodes[index];
  let rightBtnNodes = luckyBagCardNode.querySelectorAll(
    'div[class*="index-rightContent--"] button'
  ) as unknown as HTMLElement[];

  console.log(rightBtnNodes, "luckyBagCardNodes");
  rightBtnNodes[1].click();
}

/**
 * 获取福袋抽屉的Node元素
 */
export async function getLuckyBagDrawerNode(num: number) {
  let superLuckyBagDrawerNode = document.querySelector(
    'div[class*="index-superLuckyBagDrawer-"]'
  ) as HTMLElement;
  if (superLuckyBagDrawerNode) {
    return superLuckyBagDrawerNode;
  } else if (num < 50) {
    console.log("traverse循环次数：", num);
    await sleep(100);
    return getLuckyBagDrawerNode(num + 1);
  }
}
/**
 * 获取福袋抽屉中福袋列表的Node元素
 */
export async function getLuckyBagCardNodes(
  superLuckyBagDrawerNode: any,
  num: number
) {
  let luckyBagCardNodes = superLuckyBagDrawerNode.querySelectorAll(
    'div[class*="index-cardContanier-"]'
  );
  if (luckyBagCardNodes && luckyBagCardNodes.length) {
    return luckyBagCardNodes;
  } else if (num < 50) {
    console.log("traverse循环次数：", num);
    await sleep(100);
    return getLuckyBagCardNodes(superLuckyBagDrawerNode, num + 1);
  }
}
/**
 * 设置当前正在讲解的商品
 */
export async function setExplanation(
  goodsBtnNodes: any,
  btnIndex: number,
  num: number
) {
  let btnText1 = goodsBtnNodes[btnIndex].innerText;
  if (btnText1 == "讲解") {
    goodsBtnNodes[btnIndex].click();
  } else if (num < 50) {
    console.log("traverse循环次数：", num);
    await sleep(100);
    setExplanation(goodsBtnNodes, btnIndex, num + 1);
  }
}

/**
 * 延迟后续逻辑的执行时间
 */
export function sleep(time: number | undefined) {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}

export function insertTextWithCommand(textarea: any, text: any) {
  textarea.focus();
  // 使用document.execCommand（兼容老浏览器）
  document.execCommand("insertText", false, text);
}
